<template>  
    <div class="container">   
      <div class="main">  
        <div class="header">    
          <el-input placeholder="请输入关键字搜索" v-model="searchTable" style="width: 200px;"/>  
          <el-button type="info" @click="query">查询</el-button>  
          <el-button type="info" @click="query">重置</el-button>  
        </div>  
        <el-table :data="tableData" border class="eltable">  
          <el-table-column prop="pdtz_state" label="状态" width="80">
            <template #default="{row}">
              <span v-if="row.pdtz_state == 1">
                    {{zt.obj1}}
                </span>
                <span v-if="row.pdtz_state == 2">
                    {{zt.obj2}}
                </span>
                <span v-if="row.pdtz_state == 3">
                    {{zt.obj3}}
                </span>
            </template>
          </el-table-column>  
          <el-table-column prop="ckpd_code" label="盘点单号" width="150"></el-table-column>  
          <el-table-column prop="pdtz_pdlx" label="盘点类型"></el-table-column>  
          <el-table-column prop="wl_code" label="物料编码"></el-table-column>  
          <el-table-column prop="pdtz_wlmc" label="物料名称" ></el-table-column>  
          <el-table-column prop="pdtz_wllx" label="物料类型" ></el-table-column>  
          <el-table-column prop="pdtz_wldw" label="物料单位"></el-table-column>  
          <el-table-column prop="pdtz_ggxh" label="规格型号"></el-table-column> 
          <el-table-column prop="pdtz_zmkcsl" label="账面总数量"></el-table-column>
          <el-table-column prop="pdtz_pdzsl" label="盘点总数量"></el-table-column>
          <el-table-column prop="pdtz_ykzsl" label="盈亏总数"></el-table-column>
          <el-table-column prop="ck_name" label="仓库"></el-table-column>   
          <el-table-column prop="kq_name" label="库区"></el-table-column>
          <el-table-column prop="kw_name" label="库位"></el-table-column>
          <el-table-column label="操作" width="170">  
            <template #default="{ row }">  
              <span v-if="row.pdtz_state == 1">
                <el-button type="text" @click="shtg(row)">审核通过</el-button>  
                <el-button type="text" @click="shbh(row)">审核驳回</el-button>  
              </span>
              <span v-if="row.pdtz_state == 2">
                    操作完成
              </span>
              <span v-if="row.pdtz_state == 3">
                操作完成
              </span>
            </template>  
          </el-table-column>  
        </el-table>  
        <el-pagination  
          @current-change="handleCurrentChange"  
          :current-page="currentPage"  
          :page-size="pageSize"  
          :total="totalItems"  
          layout="total, prev, pager, next, jumper">  
        </el-pagination>  
      </div>  
    </div>
    <el-dialog
      v-model="centerDialogVisible"
      title="确认提示"
      width="500"
      align-center
    >
        <el-icon style="font-size: 25px;float: left;margin-left: 100px;"><QuestionFilled /></el-icon ><span style="font-size: 15px;margin-right: 100px;">是否确定审核通过选中的数据</span>
        <template #footer>
        <div class="dialog-footer">
            <el-button @click="centerDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="shtgqr">确认</el-button>
        </div>
        </template>
    </el-dialog>  
    <el-dialog
      v-model="bhDialogVisible"
      title="驳回"
      width="500"
      align-center
    >
            <el-form-item label="请输入驳回原因">
              <el-input placeholder="请输入驳回原因" v-model="bhyy"></el-input>
            </el-form-item>
        <template #footer>
        <div class="dialog-footer">
            <el-button @click="bhDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="bhqr">确认</el-button>
        </div>
        </template>
    </el-dialog>  
</template>  
  
  <script>  
  import { ref, reactive, router,computed } from 'vue';
  import axios from 'axios'  
  import { ElMessage, ElMessageBox } from 'element-plus';
  
  export default {  
    setup() {  
      const searchKey = ref('');  
      const treeData = ref([]);  
      const defaultProps = {  
        children: 'children',  
        label: 'label',  
      };  
      let zt=reactive({
        obj1:'待审核',
        obj2:'已完成',
        obj3:'已驳回',
      })
      const centerDialogVisible = ref(false); 
      const bhDialogVisible = ref(false); 
      const tableData = ref([]);  
      const shtableData = ref([]);  
      const selectedFilter = ref('');  
      const searchTable = ref('');  
      const currentPage = ref(1);  
      const pageSize = ref(10);  
      const totalItems = ref(3);  
      let bhyy=ref('')
      const add = () => {  
        // 添加逻辑  
      };  
      async function SX(){
        try {
            const response = await axios.get('/api/WMS_PDTZ/GetCXWMS_PDTZ?PageNumber='+currentPage.value+'&PageSize='+pageSize.value);
            tableData.value=response.data.cXWMS_PDTZ
            totalItems.value=response.data.zon
        } catch (error) {
          console.error("请求错误:", error);
           ElMessage.error("服务器错误，请稍后重试。");
        }
      }
      SX()
      const query = () => {  
        // 查询逻辑  
        console.log('Querying with:', searchTable.value);  
      };  
      const handleCurrentChange = (page) => {  
        currentPage.value = page;  
        SX()
        // 分页逻辑  
        console.log('Current page:', page);  
      };  
      function shtg(row){
        shtableData.value=row
        centerDialogVisible.value=true
      }
      function shbh(row){
        shtableData.value=row
        bhDialogVisible.value=true
      }
      async function shtgqr(){
        console.log(shtableData.value.pdtz_code);
        try {
            const response = await axios.put('/api/WMS_PDTZ/UpdateCXWMS_PDTZ?code='+shtableData.value.pdtz_code);
            if(response.data){
              const response1 = await axios.put('/api/WMS_KC/UpdateWMS_KCXGTZ?sl='+shtableData.value.pdtz_pdzsl+'&ckcode='+shtableData.value.ck_code+'&kqcode='+shtableData.value.kq_code+'&kwcode='+shtableData.value.kw_code+'&wlcode='+shtableData.value.wl_code);
              if(response1.data){
                const response2 = await axios.put('/api/WMS_CKPDXX/UpdateWMS_CKPDZT?code='+shtableData.value.ckpdxx_code+'&pdtzcode='+shtableData.value.pdtz_code+'&state=4')
                if(response2.data){
                  ElMessage.success("成功");
                  SX()
                  centerDialogVisible.value=false
                }
              }
              
            }
        } catch (error) {
          console.error("请求错误:", error);
           ElMessage.error("服务器错误，请稍后重试。");
        }
      }
      async function bhqr(){
        try {
            const response = await axios.put('/api/WMS_PDTZ/UpdateCXWMS_PDTZBH?code='+shtableData.value.pdtz_code+'&bhyy='+bhyy.value);
            if(response.data){
              const response = await axios.put('/api/WMS_CKPDXX/UpdateWMS_CKPDZT?code='+shtableData.value.ckpdxx_code+'&pdtzcode=NULL&state=2')
              if(response.data){
                ElMessage.success('成功')
                SX()
                bhDialogVisible.value=false
              }
            }
        } catch (error) {
          console.error("请求错误:", error);
           ElMessage.error("服务器错误，请稍后重试。");
        }
      }
      // 确保所有函数都被返回  
      return {  
        searchKey,  
        treeData,  
        defaultProps,  
        tableData,  
        selectedFilter,  
        searchTable,  
        currentPage,  
        pageSize,  
        totalItems,  
        add,   
        query,   
        handleCurrentChange,  
        shtg,
        centerDialogVisible,
        shtgqr,
        zt,
        bhDialogVisible,
        shbh,
        bhyy,
        bhqr
      };  
    },  
  };  
  </script>  
  
  <style>  
.container {  
  display: flex;  
  gap: 20px;
  height: 95%;
}  

.sidebar {  
  height: 100%;
  
}  

.main {  
  width: 80%;
  flex: 1;  
  height: 100%;
}  

.header {  
   display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}  
.eltable{
  height: 95%;
  font-size: 60%;
}
.letree{
  height: 100%;
}
</style>